package com.xindaoapp.happypet.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.Constants;

public class PictureEditorActivity extends BaseActivity implements OnClickListener {
    private ViewPager mViewPager;
    private View deletePic;
    private TextView mPhotoPlan;

    private MyGalleryAdapter mAdapter;
    private ArrayList<String> picurlList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picture_edit_layout);
        ImageLoader.getInstance().clearMemoryCache();
        picurlList = getIntent().getStringArrayListExtra(Constants.PARAM_PHOTO_LIST);
        initView();
        initDate();
    }

    private void initDate() {
        mAdapter = new MyGalleryAdapter();
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(getIntent().getIntExtra(Constants.PARAM_PHOTO_POSITION, 0));
        mViewPager.setOnPageChangeListener(mOnPageChangeListener);
        resetPlan();
    }

    private void resetPlan() {
        mPhotoPlan.setText(String.format("%s/%s", mViewPager.getCurrentItem() + 1,
                picurlList.size()));
    }

    private final OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            resetPlan();
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {}

        @Override
        public void onPageScrollStateChanged(int arg0) {}
    };

    private void initView() {
        mPhotoPlan = (TextView) findViewById(R.id.bottom_title);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        deletePic = findViewById(R.id.delete);
        deletePic.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.delete:
                if (mViewPager.getAdapter().getCount() != 0) {
                    picurlList.remove(mViewPager.getCurrentItem());
                    mAdapter.notifyDataSetChanged();
                    if (picurlList.size() == 0) {
                        save();
                    } else {
                        resetPlan();
                    }
                }
                break;
        }
    }

    private void save() {
        Intent data = new Intent();
        data.putStringArrayListExtra(Constants.PARAM_PHOTO_LIST, picurlList);
        setResult(RESULT_OK, data);
        finish();
    }

    class MyGalleryAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return picurlList.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view =
                    LayoutInflater.from(PictureEditorActivity.this).inflate(
                            R.layout.picture_edit_item, null);
            ImageLoader.getInstance().displayImage("file://" + picurlList.get(position),
                    (ImageView) view.findViewById(R.id.imageview));
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    save();
                }
            });
            container.addView(view);
            return view;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent data = new Intent();
            data.putStringArrayListExtra(Constants.PARAM_PHOTO_LIST, picurlList);
            setResult(RESULT_OK, data);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
